package com.xscha.tableforge.service;

import com.xscha.tableforge.model.vo.PageReq;
import com.xscha.tableforge.utils.EntityFieldUtil;

/**
 * 基础Service类，提供通用方法
 */
public abstract class BaseService<T> {

	/**
	 * 获取实体类Class
	 */
	protected abstract Class<T> getEntityClass();

	/**
	 * 处理分页请求中的字段名映射
	 * @param pageReq 分页请求
	 * @return 处理后的分页请求
	 */
	protected PageReq processPageRequest(PageReq pageReq) {
		if (pageReq == null) {
			return null;
		}

		// 创建新的PageReq对象，避免修改原对象
		PageReq processedReq = new PageReq();
		processedReq.setPageNum(pageReq.getPageNum());
		processedReq.setPageSize(pageReq.getPageSize());

		// 处理排序字段名映射
		if (pageReq.getOrderBy() != null) {
			String columnName = EntityFieldUtil.getColumnName(getEntityClass(), pageReq.getOrderBy());
			processedReq.setOrderBy(columnName);
		} else {
			processedReq.setOrderBy(pageReq.getOrderBy());
		}

		processedReq.setOrderDirection(pageReq.getOrderDirection());

		return processedReq;
	}
}